﻿using DotNetClient;
using Frameworks;
using RoomMsg;
using UnityEngine;
using UnityEngine.UI;

namespace Doudizhu
{
    public class DoudizhuRoom : MonoBehaviour
    {
        public GameObject player_template;
        public Text title_txt;
        public Button ready_btn;
        public Text readyBtn_txt;

        private void Start()
        {
            ready_btn.onClick.AddListener(reqReady);

            EventProxy.AddEventListener(Routes.S2C_ROOM_ON_PLAYER_INFO, onPushPlayerInfo);
            EventProxy.AddEventListener(Routes.S2C_ROOM_ON_MATCH, onPushRoomMatch);
        }

        private void OnDestroy()
        {
            EventProxy.RemoveEventListener(Routes.S2C_ROOM_ON_PLAYER_INFO, onPushPlayerInfo);
            EventProxy.RemoveEventListener(Routes.S2C_ROOM_ON_MATCH, onPushRoomMatch);
        }

        private void reqReady()
        {
            if (DoudizhuData.Instance.my.State == (int)PLAYER_STATE.SITDOWN)
            {
                c2s_playerAction msg = new c2s_playerAction() { Action = (uint)PLAYER_ACTION.READY };
                NetworkManager.Instance.Notify(Routes.C2S_ROOM_PLAYER_ACTION, msg);
            }
            else if (DoudizhuData.Instance.my.State == (int)PLAYER_STATE.READY)
            {
                c2s_playerAction msg = new c2s_playerAction() { Action = (uint)PLAYER_ACTION.SITDOWN };
                NetworkManager.Instance.Notify(Routes.C2S_ROOM_PLAYER_ACTION, msg);
            }
        }

        private void onPushPlayerInfo(EventData eventData)
        {
            if (eventData == null || eventData.target == null) return;

            s2c_onPlayerInfo data = eventData.target as s2c_onPlayerInfo;
            switch ((PLAYER_UPDATE)data.Type)
            {
                case PLAYER_UPDATE.ADD: break;
                case PLAYER_UPDATE.UPDATE_NAME: break;
                case PLAYER_UPDATE.UPDATE_STATE:
                    PlayerEntity player = DoudizhuData.Instance.GetPlayerByUid(data.Player.Uid);
                    player.UpdateState(data.Player.State);

                    if (data.Player.Uid == DoudizhuData.Instance.my.Uid)
                    {
                        if (data.Player.State == (int)PLAYER_STATE.READY) readyBtn_txt.text = "取消准备";
                        else if (data.Player.State == (int)PLAYER_STATE.SITDOWN) readyBtn_txt.text = "准备";
                    }
                    break;
                case PLAYER_UPDATE.UPDATE_OFFINE: break;
                default: break;
            }
        }

        private void onPushRoomMatch(EventData eventData)
        {
            if (eventData == null || eventData.target == null) return;

            s2c_onMatch data = eventData.target as s2c_onMatch;
            if (data.Code == (int)RETURN_CODE.SUCCESS)
            {
                title_txt.text = $"{ data.Room.Name }";

                DoudizhuData.Instance.ClearPlayers();
                for (int i = 0; i < data.Players.Count; i++)
                {
                    generatePlayer(data.Players[i]);

                    if (data.Players[i].Uid == MyselManager.Instance.Myself.Id)
                    {
                        DoudizhuData.Instance.my = data.Players[i];
                    }
                }
            }
        }

        private void generatePlayer(Player player)
        {
            GameObject playerGo = Instantiate(player_template, player_template.transform.parent);
            playerGo.SetActive(true);

            PlayerEntity playerEntity = new PlayerEntity(playerGo);
            playerEntity.Update(player);

            DoudizhuData.Instance.AddPlayer(playerEntity);
        }
    }
}
